﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://www.codeplex.com/EmlenMud
using System;
using System.Xml.Serialization;
using System.ComponentModel;
using System.Reflection;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BrainTechLLC.ThreadSafeObjects;
using System.Runtime.Serialization;

namespace BrainTechLLC
{
    [Serializable]
    [DataContract]
    public class GameCharTemplate : Template, INotifyPropertyChanged
    {
        public GameCharTemplate()
        {
            _typeName = "BrainTechLLC.GameChar";
        }

        [DataMember]
        public long _shopDataId;

        public long ShopDataId
        {
            get { return _shopDataId; }
            set
            {
                if (!_shopDataId.AreEqual(value))
                {
                    OnPropertyChanging("ShopDataId", _shopDataId, value);
                    _shopDataId = value;
                    OnPropertyChanged("ShopDataId", _shopDataId, value);
                }
            }
        }

        [DataMember]
        public string _areaName;

        public string AreaName
        {
            get { return _areaName; }
            set
            {
                if (!_areaName.AreEqual(value))
                {
                    OnPropertyChanging("AreaName", _areaName, value);
                    _areaName = value;
                    OnPropertyChanged("AreaName", _areaName, value);
                }
            }
        }

        [DataMember]
        public CommaList _abilityTemplateNames;

        public CommaList AbilityTemplateNames
        {
            get
            {
                if (_abilityTemplateNames == null)
                    Interlocked.CompareExchange<CommaList>(ref _abilityTemplateNames, new CommaList(), null);

                return _abilityTemplateNames;
            }
            set
            {
                if (_abilityTemplateNames != value)
                {
                    _abilityTemplateNames = value;
                    OnPropertyChanged(this, new PropertyChangedEventArgsEx("AbilityTemplateNames"));
                }
            }
        }

        public static GameCharTemplate FindCharTemplateByVnum(int vnum, Dictionary<int, GameCharTemplate> charTemplates)
        {
            var templates = charTemplates;
            GameCharTemplate t;

            if (charTemplates.TryGetValue(vnum, out t))
                return t;

            return null;            
        }

        public override void RegisterTemplate()
        {
            base.RegisterTemplate();
            if (ThingProperties._props == null)
            {
                ThingProperties.AddPairedProperty("HitPoints", "Max", 50, 50);
                ThingProperties.AddPairedProperty("MovementPoints", "Max", 100, 100);
                ThingProperties.AddPairedProperty("Strength", "Max", 20, 20);
                ThingProperties.AddPairedProperty("Constitution", "Max", 20, 20);
                ThingProperties.AddPairedProperty("Intelligence", "Max", 20, 20);
                ThingProperties.AddPairedProperty("Wisdom", "Max", 20, 20);
                ThingProperties.AddPairedProperty("Dexterity", "Max", 20, 20);
                ThingProperties.Props.AddOrSet("Experience", new IdentifierAndValue("Experience", 50));
                ThingProperties.Props.AddOrSet("Level", new IdentifierAndValue("Level", 1));
                ThingProperties.Props.AddOrSet("Race", new IdentifierAndValue("Race", "Human"));
            }
            if (_abilityTemplateNames == null)
            {
                _abilityTemplateNames = new CommaList("BodyTemplate", "EyesTemplate", "LegsTemplate");
            }
        }

        public override void AddDefaults(IFromTemplate item)
        {
            base.AddDefaults(item);

            GameChar chr = item as GameChar;
            List<string> abilityTemplateNames = _abilityTemplateNames == null ? new List<string>() : _abilityTemplateNames.ReadOnlyList;
            for (int n = 0; n < abilityTemplateNames.Count; n++)
            {
                string templateName = abilityTemplateNames[n];
                Template template = Template.AllTemplates[templateName];
                if (template == null)
                {
                    throw new Exception("Template with name " + templateName + " not found when trying to create item from template " + FriendlyName + ", ID " + AssociatedID.ToString());
                }
                Ability ability = template.CreateInstance<Ability>(null);
                chr.Abilities.Add(ability.AssociatedID);
            }

            State state = StateFactory.CreateBasicHealingState(4, 4, 5000);

            if (chr.States.FindStateByName(state.StateName) == null)
                chr.States.AddState(state, 5000);
        }
    }
}
